package com.study.naixue.rpccommon.protocol;

import com.study.naixue.rpccommon.entity.User;
import com.study.naixue.rpccommon.util.ByteConverter;
import lombok.Data;

import java.io.Serializable;

/**
 * @ClassName RpcProtocol
 * @Description TODO
 * @Author xuqianghui
 * @Date 2020/5/23 11:11
 * @Version 1.0
 */
@Data
public class RpcProtocol implements Serializable {

    public static int CMD_CREATE_USER = 1;

    private int version;

    private int cmd;

    private int magicNum;

    private int bodyLen = 0;

    private byte[] body;

    final public static int HEAD_LEN = 16;//


    /**
     * 序列化用户对象
     * @param info
     * @return
     */
    public byte[] userInfoToByteArray(User info){

        byte[] data = new byte[Long.BYTES + Short.BYTES + Short.BYTES];
        int index = 0;
        // 将 uid 复制到data数组 的前8个字节
        System.arraycopy(ByteConverter.longToBytes(info.getUid()), 0, data, index, Long.BYTES);
        index += Long.BYTES;
        System.arraycopy(ByteConverter.shortToBytes(info.getAge()), 0, data, index, Short.BYTES);
        index += Short.BYTES;
        System.arraycopy(ByteConverter.shortToBytes(info.getSex()), 0, data, index, Short.BYTES);
        return data;
    }

    /**
     * 序列化请求 数据包
     * @return
     */
    public byte[] generateByteArray(){
        byte[] data = new byte[HEAD_LEN + bodyLen];
        int index = 0;

        System.arraycopy(ByteConverter.intToBytes(version), 0, data, index, Integer.BYTES);

        index += Integer.BYTES;
        System.arraycopy(ByteConverter.intToBytes(cmd), 0, data, index, Integer.BYTES);

        index += Integer.BYTES;
        System.arraycopy(ByteConverter.intToBytes(magicNum), 0, data, index, Integer.BYTES);

        index += Integer.BYTES;
        System.arraycopy(ByteConverter.intToBytes(bodyLen), 0, data, index, Integer.BYTES);

        index += Integer.BYTES;
        System.arraycopy(body, index, data, 0, body.length);
        return data;
    }

    /**
     * 反序列化 数据包 到 rpcProtocol 对象
     * @param data
     * @return
     */
    public RpcProtocol byteArrayToRpcProtocol(byte[] data){
        int index = 0;
        this.setVersion(ByteConverter.bytesToInt(data, index));

        index += Integer.BYTES;
        this.setCmd(ByteConverter.bytesToInt(data, index));

        index += Integer.BYTES;
        this.setMagicNum(ByteConverter.bytesToInt(data, index));

        index += Integer.BYTES;
        this.setBodyLen(ByteConverter.bytesToInt(data, index));

        index += Integer.BYTES;

        this.body = new byte[this.bodyLen];//body参数
        System.arraycopy(data, index, this.body, 0, this.bodyLen);

        return this;


    }

    /**
     * 反序列化 到user对象
     * @param data
     * @return
     */
    public User byteArrayToUser(byte[] data){

        User user = new User();
        int index = 0;
        user.setUid(ByteConverter.bytesToLong(data, index));

        index += Long.BYTES;
        user.setAge(ByteConverter.bytesToShort(data, index));

        index += Short.BYTES;
        user.setSex(ByteConverter.bytesToShort(data, index));


        return user;
    }


}
